﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using HSS.Interlink.UI;

namespace HSS.Interlink.Client.SL5.TestApp
{
	public partial class CustomUploadDialog : ChildWindow
	{

		IEnumerable<FileInfo> fileList;

		public CustomUploadDialog()
		{
			InitializeComponent();
		}

		public CustomUploadDialog(IEnumerable<FileInfo> files)
		{
			InitializeComponent();

			this.fileList = files;

			this.uploadControl.Loaded += (s, e) =>
			{
				if (null != this.fileList && this.fileList.Count() > 0)
				{
					if (this.uploadControl.AddFiles(this.fileList))
					{
						if (this.uploadControl.AutoUpload)
							this.uploadControl.UploadAsync();
					}
				}
			};

		}

		private void ChildWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			if (this.uploadControl.IsBusy)
			{
				e.Cancel = true;
				this.uploadControl.UploadCompleted += new EventHandler(uploadControl_UploadCompleted);
				this.uploadControl.CancelAsync();
			}
		}

		private void uploadControl_UploadCompleted(object sender, EventArgs e)
		{
			this.uploadControl.UploadCompleted -= new EventHandler(uploadControl_UploadCompleted);
			this.Close();
		}

		private void uploadControl_GetMetadata(object sender, GetMetadataEventArgs e)
		{
			e.Metadata = e.FileName;
		}
	}
}

